package com.michaelbulava.TFS.actions;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.vcsUtil.VcsUtil;
import com.michaelbulava.TFS.vcs.TFSVcs;
import com.michaelbulava.TFS.vcs.utils.TFSFileUtil;

/**
 * Created by MichaelA on 8/21/2015.
 */
public class IgnoreAction extends AnAction implements DumbAware {

    /**
     * Implement this method to provide your action handler.
     *
     * @param e Carries information on the invocation place
     */
    @Override
    public void actionPerformed(AnActionEvent e) {
        Project project = (Project)e.getData(CommonDataKeys.PROJECT);
        VirtualFile[] files = VcsUtil.getVirtualFiles(e);
        for(VirtualFile file : files){
            TFSVcs.getInstance(project).addIgnoreFile(file);
        }
        TFSFileUtil.refreshAndMarkDirty(project, files, true);
    }

    @Override
    public void update(AnActionEvent e){
        Project project = (Project)e.getData(CommonDataKeys.PROJECT);
        VirtualFile[] files = VcsUtil.getVirtualFiles(e);
        e.getPresentation().setEnabled(isEnabled(project, files));
    }

    private static boolean isEnabled(Project project, VirtualFile[] files){
        for(int i = 0; i < files.length; i++){
            if(!TFSVcs.isUnderTFS(files[i], project))
                return false;
            if(TFSVcs.getInstance(project).isIgnoredFile(files[i]))
                return false;
        }
        return true;
    }
}
